package com.neu.af.dataFilter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.af.dataFilter.mapper.AbDataMapper;
import com.neu.af.dataFilter.entity.AbData;
import com.neu.af.dataFilter.entity.AbDataExample;
import com.neu.af.dataFilter.mapper.JizuMapper;
import com.neu.af.dataFilter.service.BasicAbDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("BasicAbDataService")
public class BasicAbDataServiceImpl implements BasicAbDataService {
    @Autowired
    private AbDataMapper AbDataMapper;
    @Autowired
    private JizuMapper jizuMapper;

    @Override
    public int insert(AbData record) {
        return AbDataMapper.insert(record);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return AbDataMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int countByExample(AbDataExample example) {
        return AbDataMapper.countByExample(example);
    }

    @Override
    public int updateByPrimaryKey(AbData record) {
        return AbDataMapper.updateByPrimaryKey(record);
    }

    @Override
    public AbData selectByPrimaryKey(String id) {
        return AbDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AbData> selectByExample(AbDataExample example) {
        return AbDataMapper.selectByExample(example);
    }

    @Override
    public PageInfo<AbData> OrderingAbData(Timestamp startTim, String tagName, Integer page) {
        AbDataExample AbDataExample = new AbDataExample();
        AbDataExample.Criteria criteria = AbDataExample.createCriteria();
        if (startTim != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(startTim);
        }
        if (tagName != null) {
            criteria.andTagNameEqualTo(tagName);
        }
        PageHelper.startPage(page, 5);
        PageHelper.orderBy("CREATE_TIME asc");
        List<AbData> list = AbDataMapper.selectByExample(AbDataExample);
        PageInfo pageAbData = new PageInfo(list);
        list = pageAbData.getList();
        PageHelper.startPage(page, 5, true);
        return new PageInfo<>(list);
        // return list;
    }



    @Override
    public PageInfo<AbData> ConditionSelect(Timestamp startTim, Timestamp endTime, String descriptor, Integer type, Integer alarmClass, Integer page, String jizuname) {
        AbDataExample AbDataExample = new AbDataExample();
        AbDataExample.Criteria criteria = AbDataExample.createCriteria();
        if (startTim != null) {
            if (endTime != null) {
                criteria.andCreateTimeBetween(startTim, endTime);
            } else {
                criteria.andCreateTimeGreaterThanOrEqualTo(startTim);
            }
        } else {
            if (endTime != null) {
                criteria.andCreateTimeLessThanOrEqualTo(endTime);
            }
        }
        if (descriptor != null) {
            criteria.andDescriptorLike("%"+descriptor+"%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (alarmClass != null) {
            criteria.andAlarmClassEqualTo(alarmClass);
        }
        if (jizuname !=null) {
            System.out.println(jizuname);
            criteria.andDescriptorLike("%"+jizuname+"%");
        }
            PageHelper.startPage(page, 8);
        List<AbData> list = AbDataMapper.selectByExample(AbDataExample);
        PageInfo pageAbData = new PageInfo<>(list);
        list = pageAbData.getList();
        return pageAbData;
    }

    @Override
    public List<Map> sumClassByWeek(Integer year, Integer week) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("year", year);
        map.put("week", week);

        List<Map> list = AbDataMapper.sumClassByWeek(map);
        Long a = Long.valueOf(0);
        for (Map map1 : list) {
            a = a + (Long) map1.get("count(*)");
        }
        Map mapp = new HashMap();
        mapp.put("sum", a);
        list.add(mapp);
        return list;
    }

    @Override
    public List<Map> sumTagByWeek(Integer year, Integer week) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("year", year);
        map.put("week", week);
//        System.out.println(AbDataMapper.sumTagByDay(map));
        return AbDataMapper.sumTagByWeek(map);
//        PageHelper.startPage(1,5);
//        PageHelper.orderBy("count(*) desc");
//        List<Map> list = AbDataMapper.sumTagByDay(map);
//        PageAbData pageAbData = new PageAbData(list);
//        list = pageAbData.getList();
//        return list;
    }

    @Override
    public List<Map> sumClassByDay(Integer year, Integer week) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("year", year);
        map.put("week", week);
//        System.out.println(AbDataMapper.sumTagByDay(map));
        return AbDataMapper.sumClassByDay(map);
    }

    @Override
    public List<Map> comparisonByClass(Integer year, Integer week) {
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("year", year);
        map1.put("week", week);

        //上一周
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("year", year);
        map2.put("week", week - 1);

        HashMap<String, Object> map3 = new HashMap<>();
        map3.put("year", year);
        map3.put("week", week - 4);

        List<Map> list1 = AbDataMapper.sumClassByWeek(map1);
        List<Map> list2 = AbDataMapper.sumClassByWeek(map2);
        List<Map> list3 = AbDataMapper.sumClassByWeek(map3);

        List<Map> comparison = new ArrayList<>();
        Long ring1 = null;
        for (Map map6 : list1) {
            Map result1 = new HashMap();
            for (Map map4 : list2) {
                if (map6.get("alarm_class") == map4.get("alarm_class")) {
                    ring1 = (Long) map6.get("count(*)") / (Long) map4.get("count(*)");
                    result1.put("alarm_class", map4.get("alarm_class"));
                    result1.put("环比", ring1);
                }
            }
            for (Map map5 : list3) {
                if (map6.get("alarm_class") == map5.get("alarm_class")) {
                    ring1 = (Long) map6.get("count(*)") / (Long) map5.get("count(*)");
                    result1.put("alarm_class", map5.get("alarm_class"));
                    result1.put("同比", ring1);
                }
            }
            comparison.add(result1);
        }
        return comparison;
    }

    @Override
    public List<String> getjizuName() {
        return jizuMapper.getAllJizuName();
    }
}
